package com.game.league.mine;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.game.base.app.base.BaseFragment;
import com.game.base.glideV4.ImageLoader;
import com.game.base.utils.device.DeviceUtil;
import com.game.base.view.recyclerview.RecyclerAnimUtil;
import com.game.league.R;
import com.game.league.articles.exchange.Exchange;
import com.game.league.databinding.FragmentMineBinding;
import com.game.league.entrance.user.UserMgr;
import com.game.league.entrance.user.table.User;
import com.game.league.mine.adapter.CollectionAdapter;
import com.game.league.service.InterfaceMgr;
import com.game.league.util.SkipUtil;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * 个人中心
 */
public class MineFragment extends BaseFragment<FragmentMineBinding> implements View.OnClickListener {
    private CollectionAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        binding.setClick(this);

        binding.layoutCollection.customFrameLayout.showOfIndex(0);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        binding.layoutCollection.recyclerView.setLayoutManager(layoutManager);
        adapter = new CollectionAdapter(mActivity);
        binding.layoutCollection.recyclerView.setAdapter(adapter);
        RecyclerAnimUtil.runRightAnimation(binding.layoutCollection.recyclerView);
        refreshData();
    }

    @Override
    protected void initData() {
        refreshMyArticles();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_set:
                SkipUtil.showSettingActivity(mActivity);
                break;
            case R.id.item_detail:
                SkipUtil.showFlowBillActivity(mActivity);
                break;
            case R.id.btn_copy:
                DeviceUtil.copyText(mActivity, "ID号：" + UserMgr.getCurrentUser().getObjectId());
                break;
            case R.id.item_cash:
                SkipUtil.showCashExtractActivity(mActivity);
                break;
        }
    }

    /**
     * 我的兑换物品
     */
    public void refreshMyArticles() {
        InterfaceMgr.getInstance().getMyExchangeArticles(new FindListener<Exchange>() {
            @Override
            public void done(List<Exchange> list, BmobException e) {
                if (adapter == null || list == null || list.isEmpty()) return;
                adapter.setItems((ArrayList<Exchange>) list);
                binding.layoutCollection.customFrameLayout.showOfIndex(1);
            }
        });
    }

    public void refreshData() {
        if (binding == null || binding.layoutHead == null) return;
        User user = UserMgr.getCurrentUser();
        ImageLoader.loadCircleAvatar(getContext(), user.getAvatar(), binding.layoutHead.userHead);
        binding.layoutHead.userNick.setText(user.getNickname());
        binding.layoutHead.userId.setText(user.getObjectId());
        binding.layoutHead.userGender.setVisibility(user.getGender() == 0 ? View.GONE : View.VISIBLE);
        binding.layoutHead.userGender.setBackgroundResource(user.getGender() == 1 ? R.drawable.icon_man : R.drawable.icon_woman);
    }
}
